package com.youlai.boot.modules.banner.controller;

import com.youlai.boot.modules.banner.service.BannerService;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import com.youlai.boot.modules.banner.model.BannerVO;
import com.youlai.boot.modules.banner.model.BannerPageQuery;
import io.swagger.v3.oas.annotations.Operation;
import com.youlai.boot.common.result.Result;
import com.youlai.boot.modules.banner.model.BannerForm;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.boot.common.result.PageResult;

@Tag(name = "17. 广告横幅接口", description = "广告横幅接口")
@RestController
@RequestMapping("/api/v1/banner")
@RequiredArgsConstructor
public class BannerController {

    private final BannerService bannerService;

    @Operation(summary = "广告横幅列表", description = "广告横幅列表")
    @GetMapping("/page")
    public PageResult<BannerVO> getBannerPage(BannerPageQuery query) {
        IPage<BannerVO> page = bannerService.getBannerPage(query);
        return PageResult.success(page);
    }

    @Operation(summary = "广告横幅详情", description = "广告横幅详情")
    @GetMapping("/{id}")
    public Result<BannerVO> getBanner(@PathVariable Long id) {
        BannerVO banner = bannerService.getBannerById(id);
        return Result.success(banner);
    }

    @Operation(summary = "广告横幅新增", description = "广告横幅新增")
    @PostMapping
    public Result<Long> addBanner(@RequestBody BannerForm form) {
        Long id = bannerService.save(form);
        return Result.success(id);
    }

    @Operation(summary = "获取表单")
    @GetMapping("/{id}/form")
    public Result<BannerForm> getBannerForm(@PathVariable Long id) {
        BannerForm form = bannerService.getBannerForm(id);
        return Result.success(form);
    }

    @Operation(summary = "广告横幅编辑", description = "广告横幅编辑")
    @PutMapping("/{id}")
    public Result<BannerVO> update(@PathVariable Long id, @RequestBody BannerForm form) {
        boolean result = bannerService.update(form);
        return Result.judge(result);
    }

    @Operation(summary = "广告横幅删除", description = "广告横幅删除")
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable Long id) {
        boolean result = bannerService.removeById(id);
        return Result.judge(result);
    }
}
